<template>
  <el-dialog
      :title="type === 'add' ? '添加字典数据' : '修改字典数据'"
      v-model="state.visible"
      width="400px"
  >
    <el-form :model="state.ruleForm" :rules="state.rules" ref="formRef" label-width="100px" class="good-form">
      <!--      <el-form-item label="字典类型" prop="dictCode">-->
      <!--        <el-input type="text" v-model="state.ruleForm.dictCode" :disabled="true"></el-input>-->
      <!--      </el-form-item>-->
      <el-form-item label="数据标签" prop="dictLabel">
        <el-input type="text" v-model="state.ruleForm.dictLabel"></el-input>
      </el-form-item>

      <el-form-item label="数据键值" prop="dictValue">
        <el-input type="text" v-model="state.ruleForm.dictValue"></el-input>
      </el-form-item>
      <el-form-item label="字典排序" prop="dictSort">
        <el-input type="text" v-model="state.ruleForm.dictSort"></el-input>
      </el-form-item>
      <el-form-item label="字典状态" prop="status">
        <el-radio-group v-model="state.ruleForm.status">
          <el-radio label="0">正常</el-radio>
          <el-radio label="1">停用</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="state.visible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import {reactive, ref} from 'vue'
import {useRoute} from 'vue-router'
import axios from '@/utils/axios'
import {ElMessage} from 'element-plus'

const props = defineProps({
  type: String, // 用于判断是添加还是编辑
  reload: Function // 添加或修改完后，刷新列表页
})

const formRef = ref(null)
const route = useRoute()
const state = reactive({
  visible: false,
  ruleForm: {
    id:'',
    dictId: route.params.id,
    dictCode: '',
    dictLabel: '',
    dictValue: '',
    dictSort: '',
    status: ''
  },
  rules: {
    dictLabel: [
      {required: 'true', message: '名称不能为空', trigger: ['change']}
    ],
    dictValue: [
      {required: 'true', message: '类型不能为空', trigger: ['change']}
    ]
  },
  id: ''
})
// 获取详情
const getDetail = (id) => {
  axios.get(`/dict/dataId/${id}`).then(res => {
    state.ruleForm = {
      id: res.id,
      dictId: res.dictId,
      dictCode: res.dictCode,
      dictLabel: res.dictLabel,
      dictValue: res.dictValue,
      dictSort: res.dictSort,
      status: res.status
    }
  })
}
// 开启弹窗
const open = (id) => {
  console.log("dictId", state.ruleForm.dictId)

  state.visible = true
  if (id) {
    state.id = id
    // 如果是有 id 传入，证明是修改模式
    getDetail(id)
  } else {
    // 否则为新增模式
    state.ruleForm = {
      dictId: state.ruleForm.dictId,
      dictCode: '',
      dictLabel: '',
      dictValue: '',
      dictSort: '',
      status: ''
    }
  }
}
// 关闭弹窗
const close = () => {
  state.visible = false
}
const submitForm = () => {
  console.log("type", state.ruleForm)
  formRef.value.validate((valid) => {
    if (valid) {
        console.log("paramId",state.ruleForm.dictId)
      if (props.type == 'add') {
        // 添加方法
        axios.post('/dict/data', {
          dictId: state.ruleForm.dictId,
          dictLabel: state.ruleForm.dictLabel,
          dictValue: state.ruleForm.dictValue,
          dictSort: state.ruleForm.dictSort,
          status: state.ruleForm.status
        }).then(() => {
          ElMessage.success('添加成功')
          state.visible = false
          // 接口回调之后，运行重新获取列表方法 reload
          if (props.reload) props.reload()
        })
      } else {
        // 修改方法
        axios.put('/dict/data', {
          id: state.ruleForm.id,
          dictId: state.ruleForm.dictId,
          dictLabel: state.ruleForm.dictLabel,
          dictValue: state.ruleForm.dictValue,
          dictSort: state.ruleForm.dictSort,
          status: state.ruleForm.status
        }).then(() => {
          ElMessage.success('修改成功')
          state.visible = false
          // 接口回调之后，运行重新获取列表方法 reload
          if (props.reload) props.reload()
        })
      }
    }
  })
}
defineExpose({open, close})
</script>